package com.renge.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.renge.cmsservice.entity.CrmBanner;
import com.renge.cmsservice.entity.json.BannerQueryJson;
import com.renge.cmsservice.mapper.CrmBannerMapper;
import com.renge.cmsservice.service.CrmBannerService;
import com.renge.exception.GuliException;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-15
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Override
    //轮播图后台分页显示
    public IPage<CrmBanner> getByWrapperPage(Integer current, Integer pagesize, BannerQueryJson bannerQueryJson) {
        Page<CrmBanner> p1 = new Page<>(current,pagesize);
        QueryWrapper<CrmBanner> Wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(bannerQueryJson)){
            if(!StringUtils.isEmpty(bannerQueryJson.getTitle())){
                Wrapper.like("title",bannerQueryJson.getTitle());
            }
            if(!StringUtils.isEmpty(bannerQueryJson.getBegin())){
                Wrapper.ge("gmt_create",bannerQueryJson.getBegin());
            }if(!StringUtils.isEmpty(bannerQueryJson.getEnd())){
                Wrapper.le("gmt_modified",bannerQueryJson.getEnd());
            }
        }
        //调用方法返回page
        return this.page(p1, Wrapper);
    }

    @Override
    //轮播图后台删除
    public void deleteCrmBannerById(String crmBannerId) {
        CrmBanner byId = this.getById(crmBannerId);
        //没有查到则抛出错误
        if(byId==null){
            GuliException exception = new GuliException();
            exception.setMsg("没有该id的CrmBanner");
            exception.setCode(20001);
            throw exception;
        }
        this.removeById(byId);
    }
    //首页前台轮播图展示
    @Override
    @Cacheable(value = "banner", key = "'selectIndexList'")
    public List<CrmBanner> IndexfindBanner() {
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        List<CrmBanner> bannerList = this.list(wrapper);
        return bannerList;
    }
}
